package com.foton.mybatis.factory;

import com.foton.mybatis.session.SqlSession;

import java.lang.reflect.Proxy;

/**
 * @Author RenPu
 * @Date 2023/3/29 17:49
 * @Version 1.0
 * @Description: Mapper 代理工厂
 **/
public class MapperProxyFactory<T> {

    private final Class<T> mapperInterface;


    public MapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public T newInstance(SqlSession sqlSession) {

        MapperProxy mapperProxy=new MapperProxy<T>(sqlSession,this.mapperInterface);
        return (T) newInstance(mapperProxy);
    }

    protected T newInstance(MapperProxy<T> mapperProxy) {

        return (T) Proxy.newProxyInstance(this.mapperInterface.getClassLoader(),new Class[]{this.mapperInterface},mapperProxy);


    }
}
